package ar.gov.hackaton.service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import ar.gov.hackaton.dao.CategoriaCensoDao;
import ar.gov.hackaton.dao.CensoHogarDao;
import ar.gov.hackaton.dao.ProvinciaInfo;
import ar.gov.hackaton.model.CategoriaCenso;
import ar.gov.hackaton.model.CensoHogar;

public class CensoHogarService {

	@Autowired
	private CensoHogarDao censoHogarDao;
	
	@Autowired
	private CategoriaCensoDao categoriaHogarDao;

	public ProvinciaInfo getProvinciaInfoByPartido(Long provinciaId, String categoryName) {
		CensoHogar censoHogar = censoHogarDao.getCensoHogar(provinciaId);

		try {
			Method method = CensoHogar.class.getMethod(categoryName);
			Long cantidad = (Long) method.invoke(censoHogar);

			return new ProvinciaInfo(provinciaId, cantidad);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public ProvinciaInfo getProvinciaInfoByProvinciaId(Long provinciaId, String categoryName) {
		return censoHogarDao.getCensoHogarByProvinciaId(provinciaId, categoryName);
	}

	public List<ProvinciaInfo> listProvinciasInfo(String categoryName) {
		return censoHogarDao.listCensoHogarProvincias(categoryName);
	}

	public List<String> listCategoryNames() {
		List<String> result = new ArrayList<String>();
		for (Field method : CensoHogar.class.getDeclaredFields()) {
			result.add(method.getName());
		}
		Collections.sort(result);
		return result;
	}
	
	public List<CategoriaCenso> listCategoriaCenso() {
		List<CategoriaCenso> listCategoriaCenso = categoriaHogarDao.listCategoriaCenso();
		Collections.sort(listCategoriaCenso);
		return listCategoriaCenso;
	}
}
